package cn.fl.audit.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.audit.condition.AudVisitRecordQueryCondition;
import cn.fl.audit.constant.AudStandingBookConstant;
import cn.fl.audit.dto.AudVisitRecordDTO;
import cn.fl.audit.service.IAudVisitRecordService;
import cn.fl.audit.service.IAuditApiService;
import cn.fl.audit.transform.AudVisitRecordVOTransform;
import cn.fl.audit.vo.AudVisitRecordVO;
import cn.fl.framework.base.context.CurrentThreadContext;

/**
 * @Description: 回访记录表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class AudVisitRecordServiceImpl implements IAudVisitRecordService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(AudVisitRecordServiceImpl.class);
	
	/**
	 */
	@Autowired
	private IAuditApiService auditApiService;

	@Override
	public AudVisitRecordVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		AudVisitRecordDTO dto = null;//this.自定义ApiService.selectById(id);
		return AudVisitRecordVOTransform.toVO(dto);
	}

	@Override
	public Long add(AudVisitRecordVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//return this.audVisitRecordApiService.add(AudVisitRecordVOTransform.toDTO(entity));
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.audVisitRecordApiService.removeById(id);
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return this.auditApiService.removeAudVisitRecordByIds(ids);
    }

	@Override
	public boolean modify(AudVisitRecordVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.audVisitRecordApiService.modify(AudVisitRecordVOTransform.toDTO(entity));
		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		page = this.auditApiService.searchAudVisitRecordListPage(page,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
		@SuppressWarnings("unchecked")
		List<AudVisitRecordDTO> DTOList = (List<AudVisitRecordDTO>) page.getResultList();
		List<AudVisitRecordVO> VOList = AudVisitRecordVOTransform.toVOList(DTOList);
		if(CollectionUtils.isNotEmpty(VOList)){
			List<Long> visitIdList = new ArrayList<Long>(); 
			for (AudVisitRecordVO audVisitRecordVO : VOList){
				if(null!=audVisitRecordVO.getVisitId()){
					visitIdList.add(audVisitRecordVO.getVisitId());
				}
			}
			//查询回访类型为承租人 的回访记录
			//如果回访信息外键相同 那么承租人回访的回访人员姓名=后续回访的承租人姓名  (后续回访记录列表页面需显示承租人姓名)
			AudVisitRecordQueryCondition recordQC = new AudVisitRecordQueryCondition();
			recordQC.setVisitTypeCd(AudStandingBookConstant.AUD_VISIT_TYP_CD_TEN);
			recordQC.setVisitIdList(visitIdList);
			List<AudVisitRecordDTO> tenantRecordList = this.auditApiService.searchAudVisitRecordList(recordQC, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
			for (AudVisitRecordVO audVisitRecordVO : VOList) {
				for (AudVisitRecordDTO audVisitRecordDTO : tenantRecordList) {
					if(audVisitRecordVO.getVisitId().equals(audVisitRecordDTO.getVisitId())){
						audVisitRecordVO.setTenantNm(audVisitRecordDTO.getPersonnelName());
						break;
					}
				}
			}
		}
		
		page.setResultList(VOList);
		return page;
	}

	@Override
	public boolean batchAdd(List<AudVisitRecordVO> VOList) throws BaseException {
		if(CollectionUtils.isEmpty(VOList)){
			BaseLogUtils.info(logger, "batchAdd", "批量新增/修改回访记录失败,对象为空", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.auditApiService.batchAudVisitRecordAdd(AudVisitRecordVOTransform.toDTOList(VOList));
	}
	
	
}
